﻿Imports Q7P1TTS.Lib

Public Class AssignForm
    Dim currentPage, maxPage, rowPerPage As Integer
    Dim dynamicSource As New List(Of Assignation)
    Private Sub AssignForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rowPerPage = Convert.ToInt32(My.Settings.ROW_PER_PAGE)
        listEmpSelector.DataSource = DepartmentController.load().Where(Function(d As Department) d.Name = "Operational").Take(1).SingleOrDefault().Employees.ToList()
        listComplaintSelector.DataSource = ComplaintController.loadByStatus(ComplaintStatus.Pending)
        DataBind()
    End Sub

    Private Sub btnAssign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssign.Click
        If validateData() Then
            For Each i As Integer In listComplaintSelector.SelectedIndices
                Dim fb As Complaint = listComplaintSelector.Items(i)
                Dim ass As New Assignation() With {.EmployeeID = listEmpSelector.SelectedValue, .ComplaintID = fb.ID, .CreateDate = ConvertHelper.ToUnixTimestamp(DateTime.Now)}
                AssignationController.save(ass)
                fb.Status = ComplaintStatus.UnderInvestigation
                ComplaintController.save(fb)
            Next
            listComplaintSelector.DataSource = ComplaintController.loadByStatus(ComplaintStatus.Pending)
            DataBind()
        End If
    End Sub

    Private Function validateData() As Boolean
        Dim retval As Boolean = True
        If listEmpSelector.SelectedValue <= 0 Then
            ErrorProvider1.SetError(lblEmployee, "Please select an employee")
            retval = retval And False
        Else : ErrorProvider1.SetError(lblEmployee, "")
        End If

        If listEmpSelector.SelectedItems.Count <= 0 Then
            ErrorProvider1.SetError(lblComplaint, "Please select one or more complaint")
            retval = retval And False
        Else : ErrorProvider1.SetError(lblComplaint, "")
        End If

        Return retval
    End Function

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        If grdAssignList.SelectedRows.Count <> 1 Then
            MessageBox.Show("Please select 1 record.")
        Else
            Dim obj As Assignation = grdAssignList.SelectedRows(0).DataBoundItem
            AssignationController.delete(obj)
            DataBind()
        End If
    End Sub

    Private Sub DataBind()
        Dim items As List(Of Assignation) = AssignationController.load()

        grdAssignList.AutoGenerateColumns = False
        grdAssignList.DataSource = items
        dynamicSource = items
        maxPage = Math.Ceiling(dynamicSource.Count / rowPerPage)
        cboPageSelector.Items.Clear()
        For i As Integer = 1 To maxPage Step 1
            cboPageSelector.Items.Add(i)
        Next
        If cboPageSelector.Items.Count > 0 Then
            cboPageSelector.SelectedIndex = 0
        End If

        Paging()
    End Sub

    Private Sub Paging()
        lblMaxPage.Text = "/" & maxPage.ToString()
        grdAssignList.DataSource = dynamicSource.Skip((currentPage - 1) * rowPerPage).Take(rowPerPage).ToList()

        btnFirst.Enabled = (currentPage > 1)
        btnPrev.Enabled = (currentPage > 1)
        btnNext.Enabled = (currentPage < maxPage)
        btnLast.Enabled = (currentPage < maxPage)
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        cboPageSelector.SelectedIndex = 0
    End Sub

    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        cboPageSelector.SelectedIndex = cboPageSelector.SelectedIndex - 1
    End Sub

    Private Sub cboPageSelector_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPageSelector.SelectedIndexChanged
        currentPage = Convert.ToInt32(cboPageSelector.SelectedItem)
        Paging()
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        cboPageSelector.SelectedIndex = cboPageSelector.SelectedIndex + 1
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        cboPageSelector.SelectedIndex = cboPageSelector.Items.Count - 1
    End Sub

    Private Sub grdAssignList_PreRowPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles grdAssignList.RowPrePaint
        Try
            Dim ass As Assignation = grdAssignList.Rows(e.RowIndex).DataBoundItem
            grdAssignList.Rows(e.RowIndex).Cells("Employee").Value = ass.Employee.FirstName & " " & ass.Employee.LastName
            grdAssignList.Rows(e.RowIndex).Cells("Complaint").Value = ass.Complaint.RefNumber
            grdAssignList.Rows(e.RowIndex).Cells("AssignDate").Value = ConvertHelper.ToDateTime(ass.CreateDate, "MM/dd/yyyy")
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Dim mainForm As MainForm = Me.FindForm()
        mainForm.GoToForm(New FeedbackList(2), "Pending Complaint")
    End Sub
End Class
